package manager;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.Part;
public class Utilidades {

	public static String strDateServer() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(new Date());
	}
	public static String strTimeServer() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
		return dateFormat.format(new Date());
	}
	public static java.util.Date strToDate(String s) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date fecha = null;
		try {
			fecha = dateFormat.parse(s);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fecha;
	}
	
	public static String DateToStr(java.util.Date d) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(d);
	}
	
	public static java.util.Date strToDatetime(String s) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date fecha = null;
		try {
			fecha = dateFormat.parse(s);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fecha;
	}
	
	
	public static String AhoraToString() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
		java.util.Date fechaActual = new java.util.Date();
		String fecha = dateFormat.format(fechaActual);
		return fecha;
	}
	
	public static String getTimeServer() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date fechaActual = new java.util.Date();
		String fecha = dateFormat.format(fechaActual);
		return fecha;
	}
	
	public static String StrAddDayDateTime(java.util.Date d, int dias) {
		Calendar ahoraCal = Calendar.getInstance();
		ahoraCal.add(Calendar.DAY_OF_MONTH, dias);
		
		return DateToStr(ahoraCal.getTime());
	}
	
	public static Date DateAddDayDateTime(java.util.Date d, int dias) {
		Calendar ahoraCal = Calendar.getInstance();
		ahoraCal.add(Calendar.DAY_OF_MONTH, dias);
		
		return ahoraCal.getTime();
	}
	
	public static String tiempo(int horas) {
		String cadena = "";
		
		if (Math.abs(horas) < 24) {
			cadena = horas + " horas";
		}else {
			int dias = (int)horas/24;
			int h = (horas%24);
			cadena = dias + (dias==1?" dia ":" dias ") + Math.abs(h) + " horas"; 
		}
		
		
		return cadena;
	}
	
	public static String getcontentPartText(Part input) {
        Scanner sc = null;
        String content = null;
        try {
            sc = new Scanner(input.getInputStream(), "UTF-8");
            if (sc.hasNext()) {
                content = sc.nextLine();
            } else {
                content = "";
            }
            return content;
        } catch (IOException ex) {
            Logger.getLogger(Utilidades.class.getName()).log(Level.SEVERE, ex.getMessage());
            content = null;
        }finally{
            sc.close();
        }
        return content;
    }
 
    public static String getContentTextArea(Part input) {
        StringBuilder sb = null;
        Scanner sc = null;
        try {
            sc = new Scanner(input.getInputStream(), "UTF-8");
            sb = new StringBuilder("");
            while (sc.hasNext()) {
                sb.append(sc.nextLine());
                sb.append("\n");
            }
        } catch (IOException ex) {
            Logger.getLogger(Utilidades.class.getName()).log(Level.SEVERE, ex.getMessage());
            sb = null;
        }finally{
            sc.close();
        }
        if (sb == null){
            return null;
        }else{
            return sb.toString();
        }
    }
    public static boolean guardarImagenDeProdructoEnElSistemaDeFicheros(InputStream input, String fileName)
            throws ServletException {
        FileOutputStream output = null;
        boolean ok = false;
        try {
            output = new FileOutputStream(fileName);
            int leido = 0;
            leido = input.read();
            while (leido != -1) {
                output.write(leido);
                leido = input.read();
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Utilidades.class.getName()).log(Level.SEVERE, ex.getMessage());
        } catch (IOException ex) {
            Logger.getLogger(Utilidades.class.getName()).log(Level.SEVERE, ex.getMessage());
        } finally {
            try {
                output.flush();
                output.close();
                input.close();
                ok = true;
            } catch (IOException ex) {
                Logger.getLogger(Utilidades.class.getName()).log(Level.SEVERE, "Error cerrando flujo de salida", ex);
            }
        }
        return ok;
    }
}
